package com.ruoyi.miniapp.controller.api;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.miniapp.MiniApp;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.MiniAppException;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.utils.map.SoMap;
import com.ruoyi.miniapp.domain.TMiniHistory;
import com.ruoyi.miniapp.service.IMiniAppService;
import com.ruoyi.miniapp.service.ITMiniHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/miniapp/history")
public class UserHistoryDataController extends BaseController
{
    @Autowired
    private ITMiniHistoryService tMiniHistoryService;

    /**
     * 查询体重监测历史数据列表
     */
    @GetMapping("/listByDate")
    public R listByDate()
    {
        SoMap so = getRequestSoMap();
        List<TMiniHistory> list = tMiniHistoryService.selectHistoryData(so);
        return R.ok(list,"操作成功");
    }

    /**
     * 查询最近7天的体重监测历史数据列表
     */
    @GetMapping("/listByWeek")
    public R listByWeek()
    {
        SoMap so = getRequestSoMap();
        List<TMiniHistory> list = tMiniHistoryService.selectWeekHistoryData(so);
        return R.ok(list,"操作成功");
    }

    /**
     * 新增体重监测历史数据
     */
    @PostMapping("/addWeightRecord")
    public R add()
    {
        SoMap so = getRequestSoMap();
        return R.ok(tMiniHistoryService.insertWeightRecord(so));
    }

    /**
     * 判断用户当日是否已经打卡
     */
    @GetMapping("/userRecordIsExists")
    public R userRecordIsExists()
    {
        SoMap so = getRequestSoMap();
        return R.ok(tMiniHistoryService.isUserAlreadyCheckedInToday(so));
    }


    /**
     * 用户数据分析
     */
    @GetMapping("/userAnalysis")
    public R userAnalysis()
    {
        SoMap so = getRequestSoMap();
        so.set("userId",getLoginUser().getMiniAppUser().getId());
        so.set("openId",getLoginUser().getMiniAppUser().getOpenId());
        Map result = tMiniHistoryService.historyAnalysis(so);
        return R.ok(result);
    }
}
